﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace TLCApp.StkForm
{
    public partial class PrintForm : Form
    {
        DataSet mds = new DataSet();
        string mpath = "";
        List<string> mParamete;
        public PrintForm(DataSet ds, string path, List<string> Paramete)
        {
            InitializeComponent();
            mds = ds;
            mParamete = Paramete;
            mpath = path;
        }

        private void PrintForm_Load(object sender, EventArgs e)
        {
            try
            {
                ConnectionInfo logoninfo = new ConnectionInfo(); 


            ////显示报表
           
            //  显示报表   
            string ReportPath = mpath;
            ReportDocument RpDoc = new ReportDocument();
            RpDoc.Load(ReportPath);
            SetDBLogonForReport( RpDoc);
            mds.Tables[0].TableName = "命令";
           
            //SetDBLogonForReport_get(RpDoc);
            RpDoc.SetDataSource(mds);

            ////给报表中传入参数
            //RpDoc.SetParameterValue("strSYDW", "567");
            ////报表标题
            //RpDoc.SetParameterValue("strTT", "46");
            if (mParamete!=null && mParamete.Count > 0)
            {
                foreach(string strvalues in mParamete)
                {
                    RpDoc.SetParameterValue(strvalues.Split('|')[0].ToString(), strvalues.Split('|')[1].ToString());
                }

            }
            cryreportview.ReportSource = RpDoc;
            }
             catch  (Exception ex)
            {

                pulic.Cls_Popup.MsgWarnBox(ex.ToString());
            }
           
        }
        private void SetDBLogonForReport( ReportDocument reportDocument)

        {

            ConnectionInfo logoninfo = new ConnectionInfo();
            logoninfo.ServerName = System.Configuration.ConfigurationSettings.AppSettings["rptServerName"].ToString();
            logoninfo.DatabaseName = System.Configuration.ConfigurationSettings.AppSettings["rptDatabaseName"].ToString();
            logoninfo.UserID = System.Configuration.ConfigurationSettings.AppSettings["rptUserID"].ToString();
            logoninfo.Password = System.Configuration.ConfigurationSettings.AppSettings["rptPassword"].ToString();
            Tables tables = reportDocument.Database.Tables; 
            foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables) 
            { TableLogOnInfo tableLogonInfo = table.LogOnInfo;
            tableLogonInfo.ConnectionInfo = logoninfo; 
                table.ApplyLogOnInfo(tableLogonInfo); 
            }
        }
        private void SetDBLogonForReport_get(ReportDocument reportDocument)
        {

            TableLogOnInfo logoninfo = new TableLogOnInfo();
             logoninfo.ConnectionInfo.ServerName = System.Configuration.ConfigurationSettings.AppSettings["rptServerName"].ToString();
             logoninfo.ConnectionInfo.DatabaseName = System.Configuration.ConfigurationSettings.AppSettings["rptDatabaseName"].ToString();
             logoninfo.ConnectionInfo.UserID = System.Configuration.ConfigurationSettings.AppSettings["rptUserID"].ToString();
             logoninfo.ConnectionInfo.Password = System.Configuration.ConfigurationSettings.AppSettings["rptPassword"].ToString();
            //Tables tables = reportDocument.Database.Tables;
            reportDocument.Database.Tables[0].ApplyLogOnInfo(logoninfo);
        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {

        }
    }
}
